#include <iostream>
using namespace std;
/*头文件*/
#include <fstream>
#include <stdio.h>
#include <iomanip>
#include <unistd.h>
int main()
{
#if 0
    int num = 100;
    printf("num:%d\n", num);
    printf("num:%12d\n", num);
    printf("num:%-12d\n", num);
    printf("num:123456789\n");
    cout << "num:" << setw(12) << 777 << endl;
#endif
    const char *name = "./test.txt";
#if 0
    /*打开文件：使用构造函数的方式打开文件*/
    ofstream ofs(name);
#else

    ifstream ifs;
    /*out:写方式
    app:追加方式
    默认是out和trunc：覆盖
    */
    ifs.open(name, ios::out | ios::app);
#endif
    /*判断文件是否打开成功*/
    if (!ifs.is_open())
    {
        cout << "open error" << endl;
        exit(-1);
    }

    /*读数据*/
    char buffer[100] = {0};
/*读*/
// ifs >> buffer;
/*空格就结束*/
#if 0 // 读一行
    while (ifs.getline(buffer, sizeof(buffer)))
    {
        cout << buffer << endl;
        sleep(1);
    }

    // ifs.getline(buffer, sizeof(buffer));
    // cout << buffer << endl;
    // ifs.getline(buffer, sizeof(buffer));
    // cout << buffer << endl;
    // ifs.getline(buffer, sizeof(buffer));
    cout << buffer << endl;
#endif
    /*读固定大小字节*/
    ifs.read(buffer, sizeof(buffer) - 1);
    cout << buffer;

    
    ifs.close();
    return 0;
}